package com.leochin.veasynipcamerademo;

import net.reecam.IpCamera;
import net.reecam.IpCamera.PTZ_COMMAND;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

import com.misc.objc.NSData;
import com.misc.objc.NSNotification;
import com.misc.objc.NSNotificationCenter;
import com.misc.objc.NSSelector;

public class MainActivity extends Activity implements OnTouchListener{
	
	private SurfaceView mSurface;
	private IpCamera mIPCamera;
//	private SimpleAudioTrack mAudioTrack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mSurface = (SurfaceView) findViewById(R.id.surfaceView);
		
		mIPCamera = new IpCamera("", "", "172.20.224.120", "90", "admin", "", 512);
		
		findViewById(R.id.button4).setOnTouchListener(this);
		findViewById(R.id.button5).setOnTouchListener(this);
		findViewById(R.id.button6).setOnTouchListener(this);
		findViewById(R.id.button7).setOnTouchListener(this);
		
//		mAudioTrack = new SimpleAudioTrack(8000, 
//		        AudioFormat.CHANNEL_OUT_MONO, 
//		        AudioFormat.ENCODING_PCM_16BIT);
		
//		mAudioTrack.init();

		NSNotificationCenter.defaultCenter().addObserver(
		        this, new NSSelector("onImageCallback", new Class[] {NSNotification.class}), IpCamera.IPCamera_Image_Notification, mIPCamera);
		
		NSNotificationCenter.defaultCenter().addObserver(
		        this, new NSSelector("onStatusChange", new Class[] {NSNotification.class}), IpCamera.IPCamera_CameraStatusChanged_Notification, mIPCamera);
//		NSNotificationCenter.defaultCenter().addObserver(
//		        this, new NSSelector("onAudioCallback", new Class[] {NSNotification.class}), IpCamera.IPCamera_Audio_Notification, mIPCamera);
	}
	
	public void onImageCallback(NSNotification aNotification){
	    
	    NSData data = (NSData)aNotification.userInfo().get("data");
	    
	    Canvas  canvas  =  mSurface.getHolder().lockCanvas();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data.bytes(), 0, data.length());
        
        canvas.drawBitmap(bitmap, 0, 0, new Paint());
        mSurface.getHolder().unlockCanvasAndPost(canvas);
	}
	
	public void onStatusChange(NSNotification aNotification){
		
		IpCamera.CAMERA_STATUS status = (IpCamera.CAMERA_STATUS)aNotification.userInfo().get("status");
		if(status == IpCamera.CAMERA_STATUS.CONNECTED){
			mIPCamera.play_video();
		}
	}
	
//	public void onAudioCallback(NSNotification aNotification){
//	    NSData data = (NSData)aNotification.userInfo().get("data");
//	    mAudioTrack.playAudioTrack(data.bytes(), 0, data.length());
//	    
//	    Log.d("yanjun", "audio  length="+data.length());
//	}
	
	public void onStartClicked(View v){
		mIPCamera.start();
	}
	
	
    public void onStopClicked(View v){
         mIPCamera.stop();
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub

		if (event.getAction() == MotionEvent.ACTION_UP) {

			mIPCamera.ptz_control(PTZ_COMMAND.PT_STOP);
		} else if(event.getAction() == MotionEvent.ACTION_DOWN){
			
			int key = v.getId();
			switch (key) {
			case R.id.button4: //up
				 mIPCamera.ptz_control(PTZ_COMMAND.T_UP);			
				break;
			case R.id.button5: //down
				 mIPCamera.ptz_control(PTZ_COMMAND.T_DOWN);
				break;
			case R.id.button6: //left
				mIPCamera.ptz_control(PTZ_COMMAND.P_LEFT);
				break;
			case R.id.button7: //right
				mIPCamera.ptz_control(PTZ_COMMAND.P_RIGHT);
				break;

			default:
				break;
			}
		}

		return false;
	}
}
